package com.yimi.policyservice.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yimi.policyservice.common.annotation.Auth;
import com.yimi.policyservice.common.constant.GloabConstant;
import com.yimi.policyservice.dto.PageDto;
import com.yimi.policyservice.dto.enums.FunctionCodeEnum;
import com.yimi.policyservice.dto.optr.OptrComp4Create;
import com.yimi.policyservice.dto.optr.OptrCompAccountDto;
import com.yimi.policyservice.dto.optr.OptrCompDto;
import com.yimi.policyservice.dto.policyaddr.IdsBean;
import com.yimi.policyservice.entity.base.system.SysUser;
import com.yimi.policyservice.service.OptrCompService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author jack
 * 服务公司
 */
@RestController
@RequestMapping(value = "company")
public class OptrCompController extends  BaseController {
    @Autowired
    private OptrCompService optrCompService ;

    /**
     * 创建服务公司
     * @param bean
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public OptrCompDto create(@RequestBody OptrComp4Create bean){
        OptrCompDto company =   optrCompService.create(bean.getCompany(),getLoginUser());
        SysUser user = getLoginUser();
        for(OptrCompAccountDto accountDto : bean.getSolicyAccounts()){
            setCommonColumnValues(accountDto,user,0,company.getId());
        }
        optrCompService.addCompanyAccount(bean.getSolicyAccounts());
        for(OptrCompAccountDto accountDto : bean.getAccfundAccounts()){
            setCommonColumnValues(accountDto,user,1,company.getId());
        }
        optrCompService.addCompanyAccount(bean.getAccfundAccounts());
        return  company ;
    }

    /**
     * 公司搜索
     * @return
     */
    @RequestMapping(value = "search",method = RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.COMPANY_LIST)
    public PageDto search(HttpServletRequest request , @RequestParam(defaultValue = "") String key , @RequestParam(defaultValue = "0") int type){
//        handlerPage();
        int page = Integer.valueOf(request.getParameter(GloabConstant.PAGE_NAME));
        int pageSize =Integer.valueOf( request.getParameter(GloabConstant.PAGE_SIZE));
        PageHelper.startPage(page,pageSize);
        Page<OptrCompDto> res = optrCompService.search(key,type);
        return new PageDto<OptrCompDto>(res);
    }

    /**
     * 获取所有公司列表
     * @return
     */
    @RequestMapping(value = "all",method = RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.COMPANY_LIST)
    public List<OptrCompDto> findAllCompany(){
        return  optrCompService.findAll();
    }

    /**
     * 获取所有公司列表
     * @return
     */
    @RequestMapping(value = "allPermission",method = RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.COMPANY_LIST)
    public List<OptrCompDto> findAllCompanyPermission(){
        return  optrCompService.findAllPermission(getLoginUser().getId());
    }

    /**
     * 获取公司详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public OptrComp4Create findOneCompany(@PathVariable String id){
        return  optrCompService.view(id);
    }

    /**
     * 修改公司详情
     * @param id
     * @param bean
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public void update(@PathVariable String id,@RequestBody OptrComp4Create bean){
        optrCompService.update(id,bean,getLoginUser());
    }

    /**
     * 校验服务公司名称是否存在
     * @param bean
     */
    @RequestMapping(value = "/{validateCompName}",method = RequestMethod.POST)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public int validateCompName(@RequestBody OptrCompDto bean){
       return optrCompService.validateCompName(bean.getId(),bean.getServiceType(),bean.getName());
    }



    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public void del(@RequestBody IdsBean bean){
          optrCompService.delete(bean);
    }

    /**
     * 获取公司账号
     * @return
     */
    @RequestMapping(value = "/{id}/account",method = RequestMethod.GET)
    @Auth(code = FunctionCodeEnum.COMPANY)
    public List<OptrCompAccountDto> getCompanyAccount(@PathVariable String id ,@RequestParam(defaultValue = "0") int type){
        return optrCompService.findAccountByCompanyId(id,type);
    }

    @RequestMapping(value = "/{companyId}/account/{accountId}",method = RequestMethod.GET)
    public void checkAccount(@PathVariable String companyId ,@PathVariable String accountId){
         optrCompService.checkAccount(companyId,accountId);
    }





    private void setCommonColumnValues(OptrCompAccountDto accountDto,SysUser user,int type,String companyId){
        Date now = new Date();
        accountDto.setAccountType(type);
        accountDto.setCreateTime(now);
        accountDto.setCreatorId(user.getId());
        accountDto.setCreatorName(user.getName());
        accountDto.setUpdateTime(now);
        accountDto.setUpdatorId(user.getId());
        accountDto.setUpdatorName(user.getName());
        accountDto.setOptrCompId(companyId);

    }

}
